package just.stuff.morsels.ui.morsel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;

import just.stuff.morsels.MorselApplication;
import just.stuff.morsels.objects.Morsel;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

public class EditMorselPanel extends Panel {

	private IModel<Boolean> editFlag;
	
	public EditMorselPanel( String id, final Morsel morsel, IModel<Boolean> editFlag ) {
		super( id, null );
		this.editFlag = editFlag;
		Form<Morsel> f = new Form<Morsel>( "edit", new CompoundPropertyModel<Morsel>( morsel ));
		f.add( new TextField<String>( "title").setRequired( true ) );
		f.add( new TextArea<String>( "text").setRequired( true ) );
		f.add( new AutoCompleteTextField<String>( "labels", newLabelModel(morsel) ) {
			
			@Override
			protected Iterator<String> getChoices(String input) {
				int lastSpace = input.lastIndexOf( ' ');
				String prefix = lastSpace > -1 ? input.substring( 0, lastSpace + 1 ) : "";
				String suffix = lastSpace < input.length() - 1 ? input.substring( lastSpace + 1 ) : "";
				List<String> ret = new LinkedList<String>();
				if( suffix.length() > 1 ) {
					for( String label : MorselApplication.dataSource().labels( suffix ) ) {
						ret.add( prefix+label);
					}
					
				}
				return ret.iterator();
			}
			
			@Override
			public boolean isEnabled() {
				return !morsel.isRoot();
			}
			
		}.setRequired( true ));
		f.add( new AjaxButton( "cancel" ) {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				if( morsel.getId() == null ) {
					setResponsePage( MorselPage.class );
				} else {
					EditMorselPanel.this.editFlag.setObject( false );
				}
			}
			
			protected void onError(AjaxRequestTarget target, org.apache.wicket.markup.html.form.Form<?> form) {};
		}.setDefaultFormProcessing( false ) );
		f.add( new AjaxButton( "submit" ) {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				Morsel morsel = (Morsel)form.getModelObject();
				morsel.setLastSaved( new Date() );
				MorselApplication.dataSource().save( morsel );
				EditMorselPanel.this.editFlag.setObject( false );
			}
			
			protected void onError(AjaxRequestTarget target, org.apache.wicket.markup.html.form.Form<?> form) {
				target.appendJavascript( "alert('Please fill all the fields.');");
			};
			
		} );
		add( f );
		
	}

	private IModel<String> newLabelModel(final Morsel morsel) {
		IModel<String> model = new Model<String>() {
			@Override
			public String getObject() {
				List<String> labels = new ArrayList<String>();
				if( morsel.isRoot() ) {
					labels.addAll( MorselApplication.dataSource().labels() );
				} else if( morsel.getLabels() != null ) {
					labels.addAll( morsel.getLabels() );
				}
				StringBuilder sb = new StringBuilder();
				for( int i = 0; i < labels.size(); i++ ) {
					sb.append( labels.get( i ) );
					if( i < labels.size() - 1 )
						sb.append( ' ' );
				}
				return sb.toString();
			}
			
			@Override
			public void setObject(String object) {
				String [] labels = object.split( "\\s+");
				morsel.setLabels( new TreeSet<String>( Arrays.asList( labels )));
			}
		};
		return model;
	}
	
	@Override
	public boolean isVisible() {
		return editFlag.getObject();
	}

}
